﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

using WFWS.Core;
namespace WFWS.AspNetAdapter
{
    public class WFWSWorkerRequest : HttpWorkerRequest
    {
        private WFWS.Core.HttpRequest _request;
        private WFWS.Core.HttpResponse _response;

        private WFWSWorkerRequest(WFWS.Core.HttpRequest request, WFWS.Core.HttpResponse response)
        {
            _request = request;
            _response = response;
        }

        public static WFWSWorkerRequest Create(WFWS.Core.HttpRequest request, WFWS.Core.HttpResponse response)
        {
            return new WFWSWorkerRequest(request,response);
        }

        public override void EndOfRequest()
        {
            throw new NotImplementedException();
        }

        public override void FlushResponse(bool finalFlush)
        {
            _response.Flush(finalFlush);
        }

        public override string GetHttpVerbName()
        {
            return _request.Method;
        }

        public override string GetHttpVersion()
        {
            return _request.HttpVersion;
        }

        public override string GetLocalAddress()
        {
            throw new NotImplementedException();
        }

        public override int GetLocalPort()
        {
            return 808;
        }

        public override string GetQueryString()
        {
            return _request.QueryParams.ToString();
        }

        public override string GetRawUrl()
        {
            throw new NotImplementedException();
        }

        public override string GetRemoteAddress()
        {
            return _request.RemoteEndPoint.Address.ToString();
        }

        public override int GetRemotePort()
        {
            return _request.RemoteEndPoint.Port;
        }

        public override string GetUriPath()
        {
            return _request.Uri.ToString();
        }

        public override void SendKnownResponseHeader(int index, string value)
        {
            _response.SendHeader(new HttpHeader(((System.Net.HttpResponseHeader)index).ToString(), value));
        }

        public override void SendResponseFromFile(IntPtr handle, long offset, long length)
        {
            throw new NotImplementedException();
        }

        public override void SendResponseFromFile(string filename, long offset, long length)
        {
            throw new NotImplementedException();
        }

        public override void SendResponseFromMemory(byte[] data, int length)
        {
            _response.SendBody(data, 0, length);
        }

        public override void SendStatus(int statusCode, string statusDescription)
        {
            _response.Status = (System.Net.HttpStatusCode)statusCode;
            _response.StatusDescription = statusDescription;
        }

        public override void SendUnknownResponseHeader(string name, string value)
        {
            _response.SendHeader(new HttpHeader(name, value));
        }
    }
}
